#include "Precompiled.hpp"
#include "Fsm.h"
#include "PlayerState.hpp"

std::ostream& operator<< (std::ostream& os, const OpenFileEvent& evt){
    os << " OpenFileEvent (file_name = ";
    // need convert wstring to string
    //os <<  evt.file_name ;
    return os;
}
std::ostream& operator<< (std::ostream& os, const UpdatePosEvent& evt){
    return os << " UpdatePosEvent";
}

std::ostream& operator<< (std::ostream& os, const SetStartPosEvent& evt){
    return os << " SetStartPosEvent ";
}

std::ostream& operator<< (std::ostream& os, const SetEndPosEvent& evt){
    return os << " SetEndPosEvent ";
}


PlayerFsm::PlayerFsm(UiInterface* ui): file_name_(L""), ui_(ui){
    initiate();
}

void PlayerFsm::InitPlayer(HWND hwnd){
    player_.Init(hwnd, NULL);
}
std::wstring PlayerFsm::GetFileName(){
    return file_name_;
}
///////////////////////////////////////////////////////////////////////////////
// Waiting for file state
///////////////////////////////////////////////////////////////////////////////

WaitingForFileState::WaitingForFileState(){
    std::cout << __FUNCTION__ << std::endl;
}
WaitingForFileState::~WaitingForFileState(){
    std::cout << __FUNCTION__ << std::endl;
}
fsm::result WaitingForFileState::react( const OpenFileEvent& evt){
        std::cout << __FUNCTION__ << evt << std::endl;
        context< PlayerFsm >().file_name_ = evt.file_name;
        return transit< HandlingFileState >();
}

///////////////////////////////////////////////////////////////////////////////
// Handling file state
///////////////////////////////////////////////////////////////////////////////

HandlingFileState::HandlingFileState( my_context ctx ): my_base( ctx){
    context< PlayerFsm >().player_.OpenFile(
        context< PlayerFsm >().file_name_ );
    //Update UI
    context< PlayerFsm >().ui_->SetFileInfo(
        context< PlayerFsm >().file_name_,
        GetFileLength());
}
long HandlingFileState::GetFileLength(){
    return context< PlayerFsm >().player_.GetFileLength();
}

///////////////////////////////////////////////////////////////////////////////
// Stopped state
///////////////////////////////////////////////////////////////////////////////
StoppedState::StoppedState(){
    std::cout << __FUNCTION__ << std::endl;
}
StoppedState::~StoppedState(){
    std::cout << __FUNCTION__ << std::endl;
}

